package com.yztc.orangeentertainment.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.bean.channalbean.WelcomeImageBean;
import com.yztc.orangeentertainment.config.ChannalUrlConfig;
import com.yztc.orangeentertainment.utils.Okhttputils;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by My on 2016/10/23.
 */
public class WelcomeWebActivity extends AppCompatActivity {

    @InjectView(R.id.welcome_image)
    ImageView welcomeImage;
    @InjectView(R.id.tv_tiaoguo)
    TextView tvTiaoguo;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    WelcomeImageBean bean = (WelcomeImageBean) msg.obj;
                    Glide.with(WelcomeWebActivity.this).load(bean.getSplash()).into(welcomeImage);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcomeweb);
        ButterKnife.inject(this);
        initData();
        tvTiaoguo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WelcomeWebActivity.this, MainActivity.class);
                startActivity(intent);
                tvTiaoguo.setClickable(false);
                finish();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent(WelcomeWebActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }).start();


    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Okhttputils.getRequestEnq(ChannalUrlConfig.PATH.WELCOMEWEB, WelcomeWebActivity
                            .this, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String jsonString = response.body().string();
                            String obj = JSONObject.parseObject(jsonString).getString("data");
                            String obj1 = JSONObject.parseObject(obj.toString()).getString("splash");
                            WelcomeImageBean welcomeImage = JSONObject.parseObject(obj1.toString(),
                                    WelcomeImageBean.class);
                            Message message = Message.obtain();
                            message.what = 1;
                            message.obj = welcomeImage;
                            handler.sendMessage(message);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
